import { queryUserManage ,queryUserList,DeleteUserKey,UpdataUserinfo} from '../services/api';


export default {
  namespace: 'umodels',

  state: {
 
        data: [],

        setModalVisible:false,
        messagetips:'',
        duration: 2,
        message: '',
        isShowMessage: false,

        modalType: 'add', //add or edit
        modalVisible: false,
        userName: '',
        password: '',
        realName: '',
        roleName: '',
        id:'',
        roleId:'',
        mobile:'',
        email:''

  },

  effects: {
   // *fetch():{} = fetch :function*() {}

 
   *clearUserForm({payload}, { call, put }) {
    yield put({

      type:'save',
      payload: {
        modalType: 'add', 
        userName: '',
        password: '',
        realName: '',
        roleName: '',
        mobile:'',
        email:''
        
      }
    })
  },

   *changeUserForm({payload}, { call, put }) {
    yield put({

      type:'save',
      payload
    })
  },

   *showMessage({payload}, { call, put }) {
        yield put({

          type:'save',
          payload
        })
  },

    *fetchUserAdd({payload}, { call, put }) {
     // console.log('payload1111',payload)

      let messagetips=''
      const responseRES = yield call(queryUserManage,payload)
      
      if( responseRES&&responseRES.code=='0'){
          yield put({

            type:'save',
            payload:{
              messagetips: 'success', 
              isShowMessage: true, 
              message:'Add User Success!!!',
              modalVisible: false}
          })  
          const response = yield call(queryUserList,{});
    
          let userDataList=[]
         for (let i in response.data){
    
            userDataList.push({
              key:  i,
              id:  response.data[i].id,
              userName:  response.data[i].userName,
              realName:response.data[i].realName,
              roleName:  response.data[i].roleName,
              roleId:response.data[i].roleId,
              mobile:response.data[i].mobile,
              email:response.data[i].email
              
            })
         }  
          yield put({
            type: 'save',
            payload: {data: userDataList},
          });
        }
    
    },

    *fetchUserDeleteKey({payload},{select, call,put}){
      const umodels = yield select(state => state.umodels);

      const response = yield call(DeleteUserKey,payload)
      // console.log('responseDelete',response)
      // console.log('payloadDelete',{payload})
      if(response.code=='0'){
        let currentDataList = umodels.data;
        for(let i=0;i<currentDataList.length;i++){
          if(currentDataList[i].id == payload.id){
            currentDataList.splice(i,1)
          }
        }
        yield put({
  
          type:'save',
          payload: {data: currentDataList}
        })
      }
   
    },

    *fetchUserUpdataKey({payload},{select,call,put}){
      const responseRES= yield call(UpdataUserinfo,payload)
      if(responseRES.code=='0'){
        yield put({

          type:'save',
          payload: 
          {
          messagetips: 'success',
          isShowMessage: true, 
          message:'Updata UserInfo Success!!!',
          modalVisible: false
          }})
    }
    let response = yield call(queryUserList,{});
                    yield put({
                      
                        type: 'save',
                        payload: {data: response.data},
                    })
    },



    *fetchGetUserList(payload, {call, put }){
     // console.log( ' payload2222', payload)
      let response = yield call(queryUserList,payload);
      let userDataList=[]
     for (let i in response.data){

        userDataList.push({
          key:i,

          id:  response.data[i].id,
          userName:  response.data[i].userName,
          realName:response.data[i].realName,
          roleName:  response.data[i].roleName,
          roleId:response.data[i].roleId,
          mobile:response.data[i].mobile,
          email:response.data[i].email

        })
     }  
    //  //console.log('userDataList==',userDataList)
     
    
       //console.log('response222',response)
      yield put({
        type: 'save',
        payload: {data: userDataList},
      });

    }
  }, // 此处有坑 把reducers 丢里头了，

  reducers: {
    save(state,  payload ) {
      
      return {
        ...state,
        ...payload.payload
      };
    },

    // closed(state,payload) {
    //   console.log('payload44444',payload, )
    //   console.log('state',state)

    //   console.log('payloadCCCC',payload.payload )
    //   return {
    //     ...state,
    //     messagetips:payload.payload 
    //   };
    // },
  },
};

//
// reducers: {
//   save(state, action) {
//     return {
//       ...state,
//       data: action.payload,
//     };
//   },
// },
